package com.seven.z7.logviewer.controls.panel;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.seven.z7.logviewer.xml.XMLTextAttributeParser;

/**
 * 
 * @author aprokofjev
 * 
 */
public class DDMSColouringAgent implements IColouringAgent {

	private StyleContext mStyle;
	private Map<String, Color> mAttrMap;

	public DDMSColouringAgent(File file) throws SAXException, ParserConfigurationException, IOException {
		mStyle = StyleContext.getDefaultStyleContext();
		mAttrMap = new HashMap<String, Color>();
		new XMLTextAttributeParser(file, mAttrMap).parse();
	}

	public AttributeSet getAttributeSet(String line) {
		for (String key : mAttrMap.keySet()) {
			if (line.contains(key)) {
				return mStyle.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, mAttrMap.get(key));
			}
		}
		return mStyle.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.BLACK);
	}

}
